package com.my.study.service.impl;

import com.my.study.service.CustomPermissionService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @author Carlos
 * @version 1.0.0
 * @date 2022/1/9 9:20
 */
@Service
public class CustomPermissionServiceImpl implements CustomPermissionService {


    @Override
    public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        if (null != authorities && !authorities.isEmpty()) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(request.getRequestURI());
            return authorities.contains(authority);
        }

        return false;
    }
}
